package com.charity.controller;

import com.charity.domain.User;
import com.charity.service.IUserService;
import com.charity.security.JwtUtils;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private IUserService userService;

    @Resource
    private StringRedisTemplate redisTemplate;

    @Resource
    private JwtUtils jwtUtils;

    private static final String CAPTCHA_PREFIX = "captcha:";

    @PostMapping("/register")
    public ResponseEntity<Map<String, Object>> register(@RequestBody User user) {
        Map<String, Object> response = new HashMap<>();
        user.setUserType(1);
        if (userService.register(user)) {
            response.put("code", 200);
            response.put("message", "注册成功");
        } else {
            response.put("code", 400);
            response.put("message", "用户名已存在");
        }
        return ResponseEntity.ok(response);
    }

    @PostMapping("/login")
    public ResponseEntity<Map<String, Object>> login(@RequestBody Map<String, String> loginForm, HttpServletRequest request) {
        Map<String, Object> response = new HashMap<>();
        
        // 验证码校验
        String sessionId = request.getSession().getId();
        String redisKey = CAPTCHA_PREFIX + sessionId;
        String captcha = redisTemplate.opsForValue().get(redisKey);
        String userCaptcha = loginForm.get("captcha");
        if (captcha == null || !captcha.equalsIgnoreCase(userCaptcha)) {
            response.put("code", 400);
            response.put("message", "验证码错误");
            return ResponseEntity.ok(response);
        }
        
        User user = userService.adminLogin(loginForm.get("username"), loginForm.get("password"));
        if (user != null) {
            String token = jwtUtils.generateToken(user.getUsername());
            response.put("code", 200);
            response.put("message", "登录成功");
            response.put("data", user);
            response.put("token", token);
        } else {
            response.put("code", 401);
            response.put("message", "用户名或密码错误");
        }
        return ResponseEntity.ok(response);
    }

    @GetMapping("/info")
    public ResponseEntity<Map<String, Object>> getUserInfo(@RequestParam String username) {
        Map<String, Object> response = new HashMap<>();
        User user = userService.getAdminByUsername(username);
        if (user != null) {
            response.put("code", 200);
            response.put("message", "获取成功");
            response.put("data", user);
        } else {
            response.put("code", 404);
            response.put("message", "用户不存在");
        }
        return ResponseEntity.ok(response);
    }

    @PutMapping("/update")
    public ResponseEntity<Map<String, Object>> updateUser(@RequestBody User user) {
        Map<String, Object> response = new HashMap<>();
        user.setUserType(1);
        if (userService.updateUser(user)) {
            response.put("code", 200);
            response.put("message", "更新成功");
        } else {
            response.put("code", 400);
            response.put("message", "更新失败");
        }
        return ResponseEntity.ok(response);
    }

    @GetMapping("/users")
    public ResponseEntity<Map<String, Object>> getUsers(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) Integer userType) {
        Map<String, Object> response = new HashMap<>();
        try {
            Map<String, Object> usersData = userService.getUsers(page, size, username, userType);
            response.put("code", 200);
            response.put("message", "获取成功");
            response.put("data", usersData);
        } catch (Exception e) {
            response.put("code", 500);
            response.put("message", "获取用户列表失败");
        }
        return ResponseEntity.ok(response);
    }
}